package com.zhaimen.module.product.controller.admin.homelink;

import com.zhaimen.framework.common.pojo.CommonResult;
import com.zhaimen.framework.common.pojo.PageResult;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkPageReqVO;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkCreateReqVO;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkUpdateReqVO;
import com.zhaimen.module.product.controller.admin.homelink.vo.HomeLinkUpdateStatusReqVO;
import com.zhaimen.module.product.dal.dataobject.homelink.HomeLinkDO;
import com.zhaimen.module.product.service.homelink.HomeLinkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static com.zhaimen.framework.common.exception.util.ServiceExceptionUtil.exception;

@RestController
@RequestMapping("/product/homelink")
@Slf4j
@Tag(name = "管理后台-主页链接")
@Validated
public class HomeLinkController {

    @Resource
    private HomeLinkService homeLinkService;

    @PostMapping("/create")
    @Operation(summary = "创建链接接口")
    //todo 权限设置
    //todo 对图片链接的数量需不需要限制
//    @PermitAll
    public CommonResult<Long> createHomeLink(@RequestBody @Valid HomeLinkCreateReqVO reqVO){
        Long id=  homeLinkService.createHomeLink(reqVO);
        return CommonResult.success(id);
    }
    @PutMapping("/update")
    @Operation(summary = "更新链接接口")
    //todo 权限设置
    @PermitAll
    public CommonResult<Boolean> updateHomeLink(@RequestBody @Valid HomeLinkUpdateReqVO reqVO){
        homeLinkService.updateHomeLink(reqVO);
       return CommonResult.success(true);
    }
    @GetMapping("/get")
    @Operation(summary = "获得对应的链接接口")
    @Parameter(name = "id",description = "獲得鏈接的接口",example = "1",required = true)
    //todo 权限校验
    @PermitAll
    public CommonResult<HomeLinkDO> getHomeLinkDO(@RequestParam("id") Long id){
        if(id==null){
            return CommonResult.success(null);
        }
        HomeLinkDO homeLinkDO=   homeLinkService.getHomeLinkById(id);
        return CommonResult.success(homeLinkDO);
    }
    @PutMapping("/update/status")
    @Operation(summary = "修改状态")
    //todo 修改状态
    @PermitAll
    public CommonResult<Boolean> updateHomeLinkStatus(@RequestBody HomeLinkUpdateStatusReqVO reqVO){
        homeLinkService.updateHomeLinkStatus(reqVO);
        return CommonResult.success(true);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除链接接口")
    @Parameter(name = "id",description = "刪除的鏈接接口id",example = "1",required = true)
    //todo 删除链接接口
    @PermitAll
    public CommonResult<Boolean> deleteHomeLinkById(@RequestParam("id") Long id){
        homeLinkService.deleteHomeLinkById(id);
        return CommonResult.success(true);
    }
    @GetMapping ("/page")
    @Operation(summary = "分页查询")
    //todo 分页查询
    @PermitAll
    public CommonResult<PageResult<HomeLinkDO>> getHomeLinkPage( HomeLinkPageReqVO reqVO){
        PageResult<HomeLinkDO> pageResult=homeLinkService.getHomeLinkPage(reqVO);
        return CommonResult.success(pageResult);
    }
}
